package com.doradance;

import android.content.res.Configuration;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.doradance.base.BaseActivity;
import com.doradance.common.video.videoPlay;
import com.doradance.main.demo.HpplayUISDKActivity;
import com.doradance.view.CustomDialog;
import com.hpplay.callback.ExecuteResultCallBack;
import com.hpplay.callback.HpplayWindowPlayCallBack;
import com.hpplay.callback.TransportCallBack;
import com.hpplay.link.HpplayLinkControl;
import com.yong.android.framework.utils.EasyToast;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

public class MainActivity extends BaseActivity implements TransportCallBack,ExecuteResultCallBack {
    /**
     * 官网申请到的Key
     */
    private static final String mKey = "4a2fdac3756f54a783c51b4748a1fe66";
    private static final String TAG = "MainActivity";
    private boolean bAutoSendClickFlag=false;
    private HpplayWindowPlayCallBack mHpplayWindowPlayCallBack= null;
    private String mVideoPlayingUrl;
    private long mPrevCastStopTimeStamp=0;
    @Bind(R.id.videoplayer)
    videoPlay mVideoplayer;
    private HpplayLinkControl mHpplayLinkControl;
    private int mWidth;
    private int mHeigh;
    private boolean isPlay;
    private boolean isConnect;
    @Override
    protected int getContentViewLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initToolbar() {
//        hideToolbar();
        mToolbar.setVisibility(View.GONE);
    }

    @Override
    protected void bindView() {
        super.bindView();

    }

    @Override
    protected void onViewCreated(Bundle savedInstanceState) {
        //必需继承FragmentActivity,嵌套fragment只需要这行代码
//        getSupportFragmentManager().beginTransaction().replace(R.id.fragBottomMenu, new MainBottomFragment()).commitAllowingStateLoss();
        mHpplayLinkControl = HpplayLinkControl.getInstance();
        mHpplayLinkControl.setDebug(true);
        mHpplayLinkControl.initHpplayLink(this, mKey);

        mHpplayLinkControl.setTransportCallBack(this);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        mVideoplayer.setUp("http://2449.vod.myqcloud.com/2449_22ca37a6ea9011e5acaaf51d105342e3.f20.mp4", JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, "嫂子闭眼睛");
        Glide.with(this).load("http://img4.jiecaojingxuan.com/2016/8/17/f2dbd12e-b1cb-4daf-aff1-8c6be2f64d1a.jpg").into(mVideoplayer.thumbImageView);
    }


    @OnClick({R.id.videoplayer, R.id.btn_small_video,R.id.btn_lebo,R.id.btn_play_start,R.id.btn_play_pause})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.videoplayer:
                break;
            case R.id.btn_small_video:
                break;
            case R.id.btn_lebo:
                bAutoSendClickFlag=false;
                if((0!=mPrevCastStopTimeStamp)&&(System.currentTimeMillis()-mPrevCastStopTimeStamp)<3000)
                {
                    Toast.makeText(MainActivity.this,"投屏点击太频繁,请稍后重试",Toast.LENGTH_SHORT).show();
                    return;
                }
                mPrevCastStopTimeStamp=System.currentTimeMillis();

                mVideoPlayingUrl = "http://2449.vod.myqcloud.com/2449_22ca37a6ea9011e5acaaf51d105342e3.f20.mp4";
                if (mVideoPlayingUrl == null || TextUtils.isEmpty(mVideoPlayingUrl))
                {
                    mVideoPlayingUrl="https://images.apple.com/media/cn/watch/2016/1b72bf37_d77a_4b7b_8af6_cd5a46754b92/films/made-to-move/watch-made-to-move-tft-cn-20160907_1536x640h.mp4";
                }
                Log.d(TAG,"show current playing url:"+mVideoPlayingUrl);
                //0:表示从头开始;
                if(null==mHpplayWindowPlayCallBack)
                {
                    mHpplayWindowPlayCallBack= new HpplayWindowPlayCallBack()
                    {
                        @Override
                        public void onHpplayWindowDismiss()
                        {
                            //退出SDK界面
                            Log.d(TAG, "video play finished!!");
                            mHpplayWindowPlayCallBack=null;
                        }

                        @Override
                        public void onIsConnect(boolean b)
                        {
                            Log.d(TAG, "是否成功连接到电视 "+ b);
                        }

                        @Override
                        public void onIsPlaySuccess(boolean b)
                        {
                            Log.d(TAG, "是否成功推送地址到电视 "+ b);
                        }
                    };
                    mHpplayLinkControl.showHpplayWindow(MainActivity.this, mVideoPlayingUrl, 30,mHpplayWindowPlayCallBack,HpplayLinkControl.PUSH_VIDEO );
                }
                else
                {
                    Log.d(TAG, "push video activity already request to started,then just ignore it!!");
                }
            break;

            case R.id.btn_play_start:
                //开始播放
                mHpplayLinkControl.castPlayControl(MainActivity.this, 4, true);
                break;

            case R.id.btn_play_pause:
                //暂停播放
                mHpplayLinkControl.castPlayControl(MainActivity.this, 5, false);
                break;
        }
    }

    @Override
    public void onTransportData(Object o) {
        String s = (String) o;
        Log.e(TAG,"------"+s);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //退出整个通信并释放
        mHpplayLinkControl.castDisconnectDevice();
    }

    @Override
    public void onResultDate(Object arg0, int arg1) {
        switch (arg1) {
            case 1:
                showCurseDialog((Boolean) arg0?"成功":"失败");
                break;

            case 2:
                showCurseDialog((Boolean) arg0?"成功":"失败");
                break;

            case 3:
                isConnect = (Boolean) arg0;
                showCurseDialog((Boolean) arg0?"成功":"失败");
                break;

            case 4:
                showCurseDialog((Boolean) arg0?"成功":"失败");
                break;

            case 5:
                showCurseDialog((Boolean) arg0?"成功":"失败");
                break;

            case 6:
                showCurseDialog((Boolean) arg0?"成功":"失败");
                break;

            case 7:
                showCurseDialog((Boolean) arg0?"成功":"失败");
                break;

            case 8:
                showCurseDialog((Boolean) arg0?"成功":"失败");
                break;

            case 9:
                //isBack = isBack ? false : true;
                //showCurseDialog(isBack?"当前为后台成功":"当前为前台播放");
                break;

            case 10:
                isPlay = (Boolean) arg0;
                showCurseDialog(isPlay?"播放成功":"播放失败");
                break;

            case 11:
                showCurseDialog((Boolean) arg0?"成功":"失败");
                break;

            case 12:
                showCurseDialog((Boolean) arg0?"成功":"失败");
                break;

            case 13:
                showCurseDialog((Boolean) arg0?"成功":"失败");
                break;

            case 14:
                showCurseDialog((Boolean) arg0?"成功":"失败");
                break;

            case 15:
                showCurseDialog((Boolean) arg0?"成功":"失败");
                break;

            case 16:
                showCurseDialog((Boolean) arg0?"成功":"失败");
                break;

            case 17:
                showCurseDialog((Boolean) arg0?"成功":"失败");
                break;

            case 18:
                showCurseDialog((Boolean) arg0?"成功":"失败");
                break;

            case 19:
                showCurseDialog((Boolean) arg0?"成功":"失败");
                break;
            case 20:
                showCurseDialog((Boolean) arg0?"成功":"失败");
                break;
            case 21:
                showCurseDialog((Boolean) arg0?"成功":"失败");
                break;
            case 22:
                showCurseDialog((Boolean) arg0?"成功":"失败");
                break;

            default:
                break;
        }
    }
    private void showCurseDialog(String msg) {

        try {
            int dialogW = 0;
            int dialogH = 0;

            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                dialogW = mWidth - 210;
                dialogH = mHeigh - 800;
            } else {
                dialogW = mWidth - 800;
                dialogH = mHeigh - 800;
            }


            View view = View.inflate(this, R.layout.hpplay_dialog_full_layout, null);
            TextView textView = (TextView) view.findViewById(R.id.text_log);

            textView.setText(msg);
            final CustomDialog dialog = new CustomDialog.Builder(this)
                    .setDiglogMatrix(dialogW, dialogH)
                    .setContentView(view)
                    .create();
            dialog.show();

            View installLayout = view.findViewById(com.happly.link.R.id.hpplay_install_layuot);
            TextView dialogTx = (TextView) view.findViewById(com.happly.link.R.id.hpplay_dialog_bt_tx);
            installLayout.setVisibility(View.VISIBLE);
            installLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            dialogTx.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
